package com.neusoft.elm.util;


import java.sql.*;

/**
 * JDBC封装
 * 数据库工具类
 */
public class DBUtil {

    //数据库连接URL--本数据库位于个人服务器，ip 62.234.13.26
    private static final String URL = "jdbc:mysql://62.234.13.26:3306/elm_admin?characterEncoding=utf-8";
    //数据库驱动
    private static final String DRIVER = "com.mysql.cj.jdbc.Driver";
    //用户名
    private static final String USERNAME = "root";
    //密码
    private static final String PASSWORD = "101725";

    //获取Connection
    public static Connection getConnection(){
        Connection con = null;
        //加载mysql驱动程序
        try {
            Class.forName(DRIVER);
            con =  DriverManager.getConnection(URL,USERNAME,PASSWORD);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return con;
    }

    //关闭资源
    public static void close(ResultSet rs,PreparedStatement pst,Connection con){
        if(rs!=null){
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            //释放资源
            rs = null;
        }
        if(pst!=null){
            try {
                pst.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            //释放资源
            pst = null;
        }
        if(con!=null){
            try {
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            //释放资源
            con = null;
        }

    }
}
